<template>
  <div class="hight100" v-if="indexData">
    <swiper class="main-swiper" :options="mainSwiperOptions" ref="mainSwiper">
      <swiper-slide>
        <div class="video" ref="video">
          <div class="mute" @click="changeMute">
            <i class="iconfont icon-jingyin-yuanshijituantubiao" v-if="muted"></i>
            <i class="iconfont icon-yinliang--yuanshijituantubiao" v-else></i>
          </div>
          <!-- <video-player class="video-player-box" :playsinline="true" :options="videoOption"></video-player> -->
          <img :src="poster" alt="" class="poster" v-show="sideMenuShow || playing">
          <video loop :class="{hideVideo:sideMenuShow}" webkit-playsinline="true" x-webkit-airplay="true" x5-video-player-type="h5" x5-video-player-fullscreen="true" playsinline="" controls id="top-video" :muted="muted" :src="video" ref="topvideo" style="object-fit: fill;"></video>
        </div>
        <title-card class="ani" :title="'产品中心'" v-if="!hasBottomNav" :en="'Product'" swiper-animate-effect="zoomIn" swiper-animate-duration=".5s" swiper-animate-delay=".4s"></title-card>
        <div class="ani" swiper-animate-effect="zoomIn" swiper-animate-duration=".5s" swiper-animate-delay=".4s">
          <div class="pro-type" :class="{mt20:hasBottomNav}">
            <span v-for="(item, index) in productCenter" :key="index" :class="{active: activeType === item.category.code}" @click="proSwiperChange(item.category.code)">{{item.category.name}}</span>
          </div>
          <div class="pro-info">
            <swiper class="pro-swiper" :options="proSwiperOption" ref="pro-swiper">
              <swiper-slide v-for="(item, index) in lv2Pro" :key="index" :parent-guid="item.parentGuid" :guid="item.code">
                <img :src="item.resource" alt="" class="pro-img">
                <h5 class="pro-title" @click="routerTo(item.code)">{{item.title}}</h5>
                <p class="pro-intro">{{item.brief}}</p>
              </swiper-slide>
            </swiper>
            <div class="btn prev-btn">
              <i class="iconfont icon-back"></i>
            </div>
            <div class="btn next-btn">
              <i class="iconfont icon-more"></i>
            </div>
          </div>
        </div>
      </swiper-slide>
      <swiper-slide class="bg2">
        <title-card class="title ani" swiper-animate-effect="zoomIn" swiper-animate-duration=".5s" swiper-animate-delay=".4s" :title="'公司简介'" :en="'About us'" :color="'white'"></title-card>
        <div class="desc ani" swiper-animate-effect="fadeInUpBig" swiper-animate-duration=".5s" swiper-animate-delay=".4s">
          <p>{{nav.company.description}}</p>
        </div>
      </swiper-slide>
      <swiper-slide class="shareholder">
        <title-card class="title ani" swiper-animate-effect="zoomIn" swiper-animate-duration=".5s" swiper-animate-delay=".4s" :title="'主要股东'" :en="'Shareholder'"></title-card>
        <div class="shareholder-swiper-ct ani" swiper-animate-effect="zoomIn" swiper-animate-duration=".5s" swiper-animate-delay=".4s">
          <div class="shareholder-div" v-for="(item,index) in indexData.stockholders[0].topics[0].topicItems" :key="index">
            <img :src="item.resource">
          </div>
        </div>
      </swiper-slide>
      <swiper-slide>
        <footer-screen></footer-screen>
      </swiper-slide>
    </swiper>
  </div>
</template>

<script>
import titleCard from "src/views/titleCard/titleCard";
import footerScreen from "src/components/footer/footer";
import swiperConfig from "src/mixins/swiperConfig";
import { mapGetters, mapActions } from "vuex";
import "video.js/dist/video-js.css";
import "vue-video-player/src/custom-theme.css";
import "src/style/myVideo.scss";
import request from "src/util/request";
import { videoPlayer } from "vue-video-player";

export default {
  name: "index",
  mixins: [swiperConfig],
  data() {
    return {
      videoInit: false,
      muted: true,
      playing: true,
      videoHeight: 0,
      videoWidth: 0,
      shareholderSwiperOption: {
        navigation: {
          nextEl: ".next-btn2",
          prevEl: ".prev-btn2"
        },
        pagination: {
          el: ".shareholder-pagination"
        }
      },
      productCenter: [],
      activeType: this.currentPro
    };
  },
  computed: {
    ...mapGetters(["hasBottomNav", "nav", "indexData", "sideMenuShow"]),
    videoOption() {
      return {
        height: this.videoHeight,
        width: this.videoWidth,
        autoplay: true,
        muted: true,
        sources: [
          {
            type: "video/mp4",
            src: "//video-1254132005.cosgz.myqcloud.com/hncy58_TVC.mov"
          }
        ]
      };
    },
    poster(){
      return this.indexData.banners[0].target;
    },
    video(){
      return this.indexData.banners[0].resource;
    },
    currentPro: {
      get() {
        return this.lv2Pro[0].parentGuid;
      },
      set(newValue) {
        this.activeType = newValue;
      }
    },
    proSwiperOption() {
      var vm = this;
      return {
        navigation: {
          nextEl: ".next-btn",
          prevEl: ".prev-btn"
        },
        on: {
          slideChangeTransitionStart() {
            vm.currentPro = this.el
              .querySelector(".swiper-slide-active")
              .getAttribute("parent-guid");
          },
          init() {
            // vm.currentPro = vm.productCenter[0].category.code
            // vm.currentPro = this.el.querySelector('.swiper-slide-active').getAttribute('parent-guid')
          }
        }
      };
    },
    wholetopics() {
      return this.indexData.wholetopics;
    },
    lv2Pro() {
      let pro = [this.indexData.wholetopics[1], this.indexData.wholetopics[2]];
      this.productCenter = pro;
      let lv2Pro = [];
      pro.forEach(parent => {
        parent.topics.forEach(child => {
          child.parentGuid = parent.category.code;
          lv2Pro.push(child);
        });
      });
      this.currentPro = this.productCenter[0].category.code;
      return lv2Pro;
    }
  },
  components: {
    titleCard,
    footerScreen,
    videoPlayer
  },
  watch: {
    sideMenuShow(val) {
      if (!val) {
        this.playVideo();
      } else {
        this.pauseVideo();
      }
    }
  },
  methods: {
    proSwiperChange(guid) {
      if (this.activeType === guid) return;
      let index = this._getProIndex(guid);
      this.$refs["pro-swiper"].swiper.slideTo(index);
      this.currentPro = guid;
    },
    changeMute() {
      this.muted = !this.muted;
    },
    routerTo(url) {
      if (url === "CY58_FINANCE1")
        this.$router.push({ path: "/product/PRODUCT_1/0" });
      if (url === "CY58_FINANCE2")
        this.$router.push({ path: "/product/PRODUCT_1/1" });
      if (url === "CY58_FINANCE3")
        this.$router.push({ path: "/product/PRODUCT_1/2" });
      if (url === "CY58_SCHOOL_SERVICE")
        this.$router.push({ path: "/product/PRODUCT_2/0" });
    },
    _getProIndex(guid) {
      let index;
      this.lv2Pro.some((e, i) => {
        index = i;
        return e.parentGuid === guid;
      });
      return index;
    },
    renderPro(data) {
      // let pro = [data[1], data[2]];
      // this.productCenter = pro;
      // let lv2Pro = [];
      // pro.forEach(parent => {
      //   parent.topics.forEach(child => {
      //     child.parentGuid = parent.category.code;
      //     lv2Pro.push(child);
      //   });
      // });
      // this.lv2Pro = lv2Pro;
      // this.currentPro = this.productCenter[0].category.code;
    },
    playVideo() {
      if(this.$refs.topvideo){
        this.$refs.topvideo.play();
        this.playing = false
      }
    },
    pauseVideo() {
      this.$refs.topvideo.pause();
      this.playing = true
    },
    ...mapActions(["getIndexData"])
  },
  mounted() {
    this.getIndexData().then(() => {
      this.videoHeight = this.$refs.video.offsetHeight;
      this.videoWidth = this.$refs.video.offsetWidth;
    });
  },
  created() {
    let vm = this;
    function play() {
      if (!vm.videoInit) {
        vm.playVideo();
        vm.videoInit = true;
        document.body.removeEventListener("touchstart", play);
      }
    }
    document.body.addEventListener("touchstart", play);
    document.addEventListener(
      "WeixinJSBridgeReady",
      function() {
        vm.$refs.topvideo.play();
      },
      false
    );
    // document.getElementById('top-video').onended = ()=>{
    //   vm.playing = false
    // }
  }
};
</script>

<style lang="scss" scoped>
.poster {
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  z-index: 10;
  width: 100%;
  height: rem(353);
}
.mute {
  position: absolute;
  z-index: 10;
  left: 10px;
  top: 10px;
  font-size: 30px;
  color: #fff;
  text-align: center;
  .iconfont {
    font-size: 35px;
  }
}
.video {
  position: relative;
  margin-top: rem(110);
  width: 100%;
  height: rem(353);
  background-color: #000;
  & >>> .vjs-big-play-button {
    height: 2em;
    width: 2em;
    line-height: 2em;
    border-radius: 1em;
  }
  & >>> video {
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    &.hideVideo#top-video {
      width: 1px;
      height: 1px;
    }
  }
}

#top-video {
  position: absolute;
  width: 100%;
  height: 100%;
  object-fit: fill;
}

.pro-info {
  position: relative;
  .btn {
    position: absolute;
    top: rem(170);
    width: rem(51);
    height: rem(51);
    text-align: center;
    line-height: rem(51);
    color: #afafaf;
    outline: none;
    &.swiper-button-disabled {
      opacity: 0.5;
    }
    &.prev-btn {
      left: rem(16);
    }
    &.next-btn {
      right: rem(36);
    }
    .iconfont {
      font-size: rem(80);
      outline: none;
    }
  }
}

.pro-type {
  display: flex;
  justify-content: center;
  align-items: center;
  height: rem(44);
  &.mt20 {
    margin-top: rem(20);
  }
  span {
    box-sizing: border-box;
    display: inline-block;
    margin: 0 rem(30);
    width: rem(250);
    height: rem(44);
    border-radius: rem(22);
    border: 1px solid #00aa90;
    line-height: rem(44);
    font-size: rem(25);
    text-align: center;
    color: #00aa90;
    transition: all 0.2s;
    &.active {
      color: #fff;
      background-color: #615d5c;
      border-color: #615d5c;
    }
  }
}

.pro-swiper {
  position: relative;
  margin-top: rem(35);
  width: rem(560);
  height: rem(500);
  .pro-img {
    display: block;
    margin: 0 auto;
    // width: 100%;
    height: rem(280);
  }
}

.pro-title {
  margin-top: rem(16);
  line-height: rem(48);
  font-size: rem(28);
  text-align: center;
  color: #333;
  border-bottom: 1px solid #333;
}

.pro-intro {
  margin-top: rem(13);
  line-height: rem(44);
  font-size: rem(27);
  color: #999;
  text-align: left;
}

.bg2 {
  background-image: url("../../assets/images/co-bg.jpg");
  background-size: cover;
  .title {
    margin-top: rem(244);
  }
  .desc {
    margin: rem(88) auto 0;
    width: rem(654);
    line-height: rem(56);
    font-size: rem(28);
    text-indent: 2em;
    color: #fff;
  }
}

.shareholder {
  .title {
    margin-top: rem(244);
  }
  .btn2 {
    position: absolute;
    z-index: 10;
    top: rem(101);
    display: block;
    width: rem(51);
    height: rem(51);
    text-align: center;
    line-height: rem(51);
    color: #afafaf;
    outline: none;
    &.swiper-button-disabled {
      opacity: 0.5;
    }
    &.prev-btn2 {
      left: rem(16);
    }
    &.next-btn2 {
      right: rem(16);
    }
    .iconfont {
      font-size: rem(70);
      outline: none;
    }
  }
  .shareholder-pagination {
    position: absolute;
    left: 50%;
    z-index: 10;
    top: rem(280);
    transform: translate(-50%, 0);
    text-align: center;
    & /deep/ .swiper-pagination-bullet {
      margin: 0 rem(13.5);
      background-color: #dedede;
      &.swiper-pagination-bullet-active {
        background-color: #c52720;
      }
    }
  }
}

.shareholder-swiper-ct {
  display: flex;
  flex-wrap: wrap;
  margin-top: rem(34);
  .shareholder-div {
    margin-bottom: rem(80);
    text-align: center;
    width: 50%;
    &:nth-child(1) {
      width: 100%;
      img {
        width: rem(280);
      }
    }
    img {
      width: rem(340);
    }
    p {
      margin-top: rem(67);
      line-height: rem(80);
      font-size: rem(36);
      color: #666;
      text-align: center;
    }
  }
}
</style>
